package cycling.taxi.com.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import java.util.Random;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cycling.taxi.com.IConstants;
import cycling.taxi.com.R;
import cycling.taxi.com.person.ExitApplication;
import cycling.taxi.com.request.Config;
import cycling.taxi.com.request.RM;
import cycling.taxi.com.request.RequestBean;
import cycling.taxi.com.request.RequestCallback;
import cycling.taxi.com.request.RequestParams;
import cycling.taxi.com.request.RequestUtil;
import cycling.taxi.com.utils.LogUtil;
import cycling.taxi.com.utils.MD5Util;
import cycling.taxi.com.utils.PhoneUtils;
import cycling.taxi.com.utils.ToastUtil;
import cycling.taxi.com.utils.Urls;

public class ForgetPasswordActivity extends AppCompatActivity implements View.OnClickListener{
    @InjectView(R.id.back)
    ImageView back;
    @InjectView(R.id.title_text)
    TextView title_text;
    @InjectView(R.id.mobile)
    TextView mobile;
    @InjectView(R.id.val)
    TextView val;
    @InjectView(R.id.xiayibu)
    Button next;
    @InjectView(R.id.validate)
    Button validate;
    //初始化验证码
    private String rad="()*";
    //volley请求队列
    private RequestQueue queue;
    //初始化验证请求时间
    private int timeC = 0;
    //初始化手机验证状态0:不存在手机号1：存在
    private String status="0";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_password);
        ButterKnife.inject(this);
        queue= Volley.newRequestQueue(this);
        ExitApplication.getInstance().addActivity(this);
        InitView();
        InitListener();
    }

    private void InitView() {
        title_text.setText("忘记密码");
    }
    private void InitListener(){
        next.setOnClickListener(this);
        back.setOnClickListener(this);
        validate.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back:
                finish();;
                break;
            case R.id.xiayibu:
                if (TextUtils.isEmpty(getverifyCode())||getverifyCode().length()<4||!getverifyCode().equals(rad)){
                    ToastUtil.getInstance().toast("验证码不正确");
                    break;
                }
                volleyPhone();
                break;
            case R.id.validate:
                //识别验证码
                reqIdentifyCode();
                break;
        }
    }
    //识别验证码
    private void reqIdentifyCode() {
        String phone = getPhone();
        if (!PhoneUtils.isMobileNO(phone)) {
            ToastUtil.getInstance().toast("手机号码不正确");
            return;
        }
        rad = RandomCode();
        volleyGet();
       ;

    }
    //获取输入手机号
    private String getPhone() {
        return mobile.getText().toString().trim();
    }
    private String getverifyCode() {
        return val.getText().toString().trim();
    }
    //生成随机验证码
    private String RandomCode(){
        String str="0123456789";
        StringBuilder sb=new StringBuilder(4);
        for(int i=0;i<4;i++)
        {
            char ch=str.charAt(new Random().nextInt(str.length()));
            sb.append(ch);
        }
        return sb.toString();
    }
    //验证码请求
    public void volleyGet(){
        //String path= Urls.getTest()+"?content="+rad;
        String mobile=getPhone();
        String path =Urls.getValidate()+mobile.trim()+Urls.getValidateContent()+rad+Urls.getValidateElse();
       // String path="https://dx.ipyy.net/smsJson.aspx?action=send&userid=&account=AG00031&password=AG00031523&mobile="+mobile+"&content=您的验证码："+rad+"【塔西】&sendTime=&extno=";
        // String path = Urls.getRepair();18677365372
        StringRequest request = new StringRequest(path, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                LogUtil.e(s);
             //   ToastUtil.getInstance().toast(s);
                //请求成功的话加上以下代码
                startColdTime();
            }
        },null);
        queue.add(request);
    }
    //手机号验证
    public void volleyPhone(){
        String path= Urls.getValPhone()+"?phone="+getPhone();
        StringRequest request = new StringRequest(path, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
               status=s;
                if(status.equals("1")) {
                    startToAty(ChangePasswordActivity.class);
                    finish();
                }
                else {
                    ToastUtil.getInstance().toast("手机号不存在");
                }

            }
        },null);
        queue.add(request);
    }
    //验证码间隔时间
    private void startColdTime() {
        timeC = 60;
        coldTimeing();
    }
    //验证码间隔时间
    private void coldTimeing() {
        if (validate == null)
            return;
        setColdTime(timeC);
        if (timeC > 0) {
            validate.postDelayed(new Runnable() {
                @Override
                public void run() {
                    timeC--;
                    coldTimeing();
                }
            }, 1000);
        }
    }
    /**
     * @param num 倒计时时间, 如果小于等于0, 显示重新发送
     */
    private void setColdTime(int num) {
        if (validate == null) {
            return;
        }
        validate.setEnabled(num <= 0);
        if (num > 0) {
            validate.setText(num + "s");
        } else {
            validate.setText(R.string.str_register_resend);
        }
    }
    //界面跳转
    private void startToAty(Class claz) {
        Intent intent = new Intent(this, claz);
        intent.putExtra("phone",getPhone());
        startActivity(intent);
    }
}
